import com.alibaba.alimonitor.jmonitor.utils.StringUtils
import com.alibaba.moco.RedisPoolInstance
import com.alibaba.moco.util.BaseTypeUtil
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import redis.clients.jedis.Jedis
import script.GlobalValues
import script.moco.dto.AutoCallbackRequest
import script.moco.dto.AutoCallbackResponse
import script.v1.didiCallback.NoticeCallback
import script.v1.didiCallback.PayNotifyRequest
//模拟48小时自动扣款回调
def excute(data) {

    Jedis redis = RedisPoolInstance.getRedisClient();
    def params = data.getParameterMap();
    def request = BaseTypeUtil.mapToBean(params, AutoCallbackRequest.class) as AutoCallbackRequest;//请求数据转换为对象

    def response = new AutoCallbackResponse();

    def jsonStr = redis.get(GlobalValues.KEY_ORDER + request.getOrderId());

    if (jsonStr == null) {    //验证订单是否存在
        response.setSuccess("false");
        response.setErrorMsg("订单id不存在 !");
        response.setErrorCode(39001);
        response.setErrorLevel(1);

        return response;
    }

    def slurper = new JsonSlurper();
    def detailMap = slurper.parseText(jsonStr);

    if (!detailMap.containsKey("order")) {
        response.setSuccess("false");
        response.setErrorMsg("订单不存在 !");
        response.setErrorCode(39001);
        response.setErrorLevel(1);

        return response;
    }
    def orderStr = redis.get(GlobalValues.KEY_ORDER + request.getOrderId())
    def orderJson = slurper.parseText(orderStr)
    orderJson.order.status = 700

    redis.set(GlobalValues.KEY_ORDER + request.getOrderId(), JsonOutput.toJson(detailMap).toString())    //直接覆盖原有数据，进行修改

    //支付回调
    NoticeCallback noticeCallbackTool = new NoticeCallback();
    PayNotifyRequest payNotifyRequest = new PayNotifyRequest();
    payNotifyRequest.setOrderId(request.getOrderId());
    def info = redis.get(GlobalValues.KEY_CALLBACK_INFO + request.getOrderId());
    payNotifyRequest.setCallbackInfo(info);
    payNotifyRequest.setNormalDistance(20);
    payNotifyRequest.setStatus(700)
    payNotifyRequest.setTotalPrice(new BigDecimal(orderJson.price.total_price).floatValue())
    String url = noticeCallbackTool.getCallbackUrlFromRequest(data);
    if (StringUtils.isNotBlank(url)) {
        response.setContent(noticeCallbackTool.handPayNotify(url, payNotifyRequest));
        response.setSuccess("true");
        response.setErrorMsg(null);
        response.setErrorCode(0);
    } else {
        response.setSuccess("false");
        response.setErrorMsg("session 中不存在回调地址");
        response.setErrorCode(-1);
    }
    response.setErrorLevel(0);

    return response;
}